package wangchao.validate.filter;

import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import wangchao.tools.StringX;
import wangchao.validate.Constant;
import wangchao.validate.ValidateType;

import static com.sun.org.apache.xalan.internal.xsltc.compiler.util.ErrorMsg.ERROR_MSG;
import static wangchao.validate.filter.DataValidateFilter.KEY;

/**
 * 具体验证接口
 * Created by ashin on 2018/11/2.
 */
public class SpecificValidationAdpterImp implements SpecificValidationAdapter {

    protected static final Logger logger = LoggerFactory.getLogger(DataValidate.class);


    /**
     * 自定义完整说明
     *
     * @param validateType
     * @param customInstructions
     * @param specify
     * @param objectKey
     * @return
     */
    @Override
    public JSONObject returnCustomInstructionsFull(ValidateType validateType, String customInstructions, int specify, String objectKey) {
        return returnCustomInstructionsFull(validateType, customInstructions, specify, objectKey, null);
    }

    @Override
    public JSONObject returnCustomInstructionsFull(ValidateType validateType, String customInstructions, int specify, String oKey, String regex) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put(KEY, oKey);
        jsonObject.put(ERROR_MSG, returnCustomInstructions(validateType, customInstructions, specify, regex));
        return jsonObject;
    }

    @Override
    public String returnCustomInstructions(ValidateType validateType, String customInstructions, int specify, String regex) {
        if (!StringX.isEmpty(customInstructions)) {
            return customInstructions;
        }
        String customSpecifyInstructions = getCustomSpecify(specify, validateType, regex);
        if (customSpecifyInstructions != null) {
            return customSpecifyInstructions;
        }
        return validateType.toString();
    }

    @Override
    public boolean checkNumLength(ValidateType validateType, int specify, double checkNum) {
        logger.info("-------长度(大小)验证开始------");
        logger.info("验证长度(大小)类型：{}", validateType.name());
        logger.info("规定长度(大小)：{}，需验证参数的长度(大小)：{}", specify, checkNum);
        if (specify < 0)
            return false;
        if (validateType.equals(ValidateType.MAX_LENGTH))
            return checkNum <= specify;
        if (validateType.equals(ValidateType.MIN_LENGTH))
            return checkNum >= specify;
        return true;
    }

    @Override
    public boolean checkRegex(String regEx, String value) {
        return regex(regEx, value);
    }

    /**
     * 返回返回值特殊判斷
     *
     * @return
     */
    String getCustomSpecify(int specify, ValidateType validateType, String regex) {
        if (validateType.equals(ValidateType.MAX_LENGTH))
            return validateType.toString() + "," + Constant.MAX_SPECIFY_MSG + "：" + specify;
        if (validateType.equals(ValidateType.MIN_LENGTH))
            return validateType.toString() + "," + Constant.MIN_SPECIFY_MSG + "：" + specify;
        if (validateType.equals(ValidateType.REGEX))
            return validateType.toString() + "," + Constant.REGEX_MSG + "：" + regex;
        return null;
    }

    /**
     * 验证正则是否正确
     *
     * @param regEx
     * @return
     */
    boolean regex(String regEx, String value) {
        logger.info("正则表达式为：{}", regEx);
        logger.info("待验证字符串：{}", value);
        boolean matches = value.matches(regEx);
        logger.info("验证结果：{}", matches ? "正确" : "错误");
        return matches;
    }

}
